// @ts-ignore
import { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/Home.vue'),
    meta: {
      title: '文件历史管理器',
      requiresAuth: false
    }
  },
  {
    path: '/history',
    name: 'History',
    component: () => import('@/views/HistoryBrowser.vue'),
    meta: {
      title: '历史浏览器',
      requiresAuth: false
    }
  },
  {
    path: '/history/:versionId',
    name: 'HistoryVersion',
    component: () => import('@/views/HistoryBrowser.vue'),
    props: true,
    meta: {
      title: '版本详情',
      requiresAuth: false
    }
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/Search.vue'),
    meta: {
      title: '搜索文件',
      requiresAuth: false
    }
  },
  {
    path: '/search/:query',
    name: 'SearchResults',
    component: () => import('@/views/Search.vue'),
    props: true,
    meta: {
      title: '搜索结果',
      requiresAuth: false
    }
  },
  {
    path: '/diff',
    name: 'Diff',
    component: () => import('@/views/DiffViewer.vue'),
    meta: {
      title: '文件对比',
      requiresAuth: false
    }
  },
  {
    path: '/diff/:versionA/:versionB/:fileId',
    name: 'DiffCompare',
    component: () => import('@/views/DiffViewer.vue'),
    props: true,
    meta: {
      title: '版本对比',
      requiresAuth: false
    }
  },
  {
    path: '/recovery',
    name: 'Recovery',
    component: () => import('@/views/Recovery.vue'),
    meta: {
      title: '文件恢复',
      requiresAuth: false
    }
  },
  {
    path: '/recovery/:versionId/:fileId',
    name: 'RecoveryFile',
    component: () => import('@/views/Recovery.vue'),
    props: true,
    meta: {
      title: '恢复文件',
      requiresAuth: false
    }
  },
  // 404 页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue'),
    meta: {
      title: '页面未找到',
      requiresAuth: false
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

// 路由守卫
router.beforeEach((to: any, _from: any, next: any) => {
  // 设置页面标题
  if (to.meta?.title) {
    document.title = `${to.meta.title} - 文件历史管理器`
  }

  // 权限检查（目前所有页面都不需要认证）
  if (to.meta?.requiresAuth) {
    // 这里可以添加认证逻辑
    // 例如检查用户登录状态
    next()
  } else {
    next()
  }
})

// 路由错误处理
router.onError((error: any) => {
  console.error('Router error:', error)
  // 可以在这里添加错误通知
})

export default router